#ifndef A8_LOCK_H
#define A8_LOCK_H

#ifdef _WIN32
#include <windows.h>
#else
#include <pthread.h>
#endif

namespace a8
{
  class Mutex
  {
  public:
    Mutex()
    {
      #ifdef _WIN32
      InitializeCriticalSection(&mHandle);
      #else
      pthread_mutex_init(&mHandle, NULL);
      #endif
    }
    ~Mutex()
    {
      #ifdef _WIN32
      DeleteCriticalSection(&mHandle);
      #else
      pthread_mutex_destroy(&mHandle);
      #endif
    }

    inline int TryLock()
    {
      #ifdef _WIN32
      TryEnterCriticalSection(&mHandle);
      #else
      pthread_mutex_trylock(&mHandle);
      #endif
    }
    inline void Lock()
    {
      #ifdef _WIN32
      EnterCriticalSection(&mHandle);
      #else
      pthread_mutex_lock(&mHandle);
      #endif
    }
    inline void UnLock()
    {
      #ifdef _WIN32
      LeaveCriticalSectioni(&mHandle);
      #else
      pthread_mutex_unlock(&mHandle);
      #endif
    }
  private:
    #ifdef _WIN32
    CRITICAL_SECTION  mHandle;
    #else
    pthread_mutex_t mHandle;
    #endif
  };
}

#endif
